<template>
  <div style="padding-left: 10%;padding-right: 10%;">
    <Row>
      <i-col span="4">
        <Menu style="margin-top: 50px;" active-name="1-3">
          <MenuItem name="1-1" to="/index/ucenter/myClass" style="font-size: 20px;">我的课程</MenuItem>
          <MenuItem name="1-2" to="/index/ucenter/myOrder" style="font-size: 20px;">订单中心</MenuItem>
          <MenuItem name="1-3" to="/index/ucenter/settings" style="font-size: 20px;">个人设置</MenuItem>
        </Menu>
      </i-col>
      <i-col span="20" style="padding-left: 45px;margin-top: 50px;">
        <Card>
          <Form :model="regForm" :label-width="80" style="width: 380px">
            <FormItem label="手机号">
              <Row>
                <Col span="16">
                  <Input v-model="regForm.mobile" placeholder="请输入手机号"/>
                </Col>
                <Col span="5">
                  <Button style="width: 110px" type="success" v-show="sendAuthCode" @click="searchCode">获取验证码</Button>
                  <Button style="width: 110px" disabled v-show="!sendAuthCode">{{auth_time}}秒后重新发送</Button>
                </Col>
              </Row>
            </FormItem>
            <FormItem label="验证码">
              <Input v-model="regForm.code" placeholder="请输入手机验证码"/>
            </FormItem>
            <FormItem label="新密码">
              <Input v-model="regForm.newPass" type="password" placeholder="请输入新密码"/>
            </FormItem>
            <Button style="margin-left: 60px;padding: 10px 30px" @click="editor" type="warning">确认修改</Button>
          </Form>
        </Card>
      </i-col>
    </Row>
  </div>
</template>

<script>
export default {
  name: 'index',
  data() {
    return {
      regForm: {},
      sendAuthCode: true,
      auth_time: 0
    }
  },
  methods: {
    searchCode() {
      if (!this.regForm.mobile) {
        this.$Message.error('请输入手机号')
        return false
      }
      this.$post('/api/api/student/modifyPasswordCode', this.$qs.stringify({mobile: this.regForm.mobile})).then((res) => {
        if (res.code === 0) {
          if (res.data.code === 20004) {
            this.$Message.error(res.data.errorMessage)
            return false
          } else {
            this.$Message.success('发送成功')
            this.getAuthCode()
          }
        }
      })
    },
    getAuthCode() {
      this.sendAuthCode = false
      this.auth_time = 300
      let authtimetimer = setInterval(() => {
        this.auth_time--
        if (this.auth_time <= 0) {
          this.sendAuthCode = true
          clearInterval(authtimetimer)
        }
      }, 1000)
    },
    editor() {
      if (!this.regForm.newPass) {
        this.$Message.error('请输入新密码')
        return false
      } else if (!this.regForm.code) {
        this.$Message.error('请输入验证码')
        return false
      } else {
        this.$post('/api/api/student/modifyPassword', this.$qs.stringify({
          newPassword: this.regForm.newPass,
          code: this.regForm.code
        })).then((res) => {
          if (res.data.code === 20004) {
            this.$Message.error(res.data.errorMessage)
            return false
          } else {
            this.$Message.success('修改成功，下次登录请使用新密码')
          }
        })
      }
    }
  }
}
</script>

<style scoped>
  .time{
    font-size: 14px;
    font-weight: bold;
  }
  .content{
    padding-left: 5px;
  }
</style>
